package com.hdjk.view.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.hdjk.view.entity.BankEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * SharedPreferences的一个工具类，调用setParam就能保存String, Integer, Boolean, Float, Long类型的参数
 * 同样调用getParam就能获取到保存在手机里面的数据
 */
public class SharedPreferencesUtil {
    private static final String HDCF_CONTENT = "hdcf_content";
    private static final String BANK_LIST = "bank_list";

    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     *
     * @param context
     * @param key
     * @param object
     */
    public static void setParam(Context context, String fill_name, String key, Object object) {

        String type = object.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(fill_name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        if ("String".equals(type)) {
            editor.putString(key, (String) object);
        } else if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) object);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) object);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) object);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) object);
        }

        editor.commit();
    }


    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     *
     * @param context
     * @param key
     * @param defaultObject
     * @return
     */
    public static Object getParam(Context context, String fill_name, String key, Object defaultObject) {
        String type = defaultObject.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(fill_name, Context.MODE_PRIVATE);

        if ("String".equals(type)) {
            return sp.getString(key, (String) defaultObject);
        } else if ("Integer".equals(type)) {
            return sp.getInt(key, (Integer) defaultObject);
        } else if ("Boolean".equals(type)) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        } else if ("Float".equals(type)) {
            return sp.getFloat(key, (Float) defaultObject);
        } else if ("Long".equals(type)) {
            return sp.getLong(key, (Long) defaultObject);
        }

        return null;
    }

    /**
     * 移除某个key值已经对应的值
     *
     * @param context
     * @param key
     */
    public static void remove(Context context, String fill_name, String key) {
        SharedPreferences sp = context.getSharedPreferences(fill_name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }

    /**
     * 清除所有内容
     *
     * @param context
     */
    public static void clear(Context context, String fill_name) {
        SharedPreferences sp = context.getSharedPreferences(fill_name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }

    public static void saveBankList(Context context, String json) {
        setParam(context, HDCF_CONTENT, BANK_LIST, json);
    }

    public static List<BankEntity> getBankList(Context context) {
        List<BankEntity> list = new ArrayList<>();
        String bank = (String) getParam(context, HDCF_CONTENT, BANK_LIST, "");
        if (!TextUtils.isEmpty(bank)) {
            try {
                JSONArray array = new JSONArray(bank);
                if (array.length() > 0) {
                    for (int j = 0; j < array.length(); j++) {
                        JSONObject jsonObject = array.getJSONObject(j);
                        BankEntity entity = new BankEntity();
                        if (!jsonObject.isNull("bankCode")) {
                            entity.setBankCode(jsonObject.getString("bankCode"));
                        }
                        if (!jsonObject.isNull("bankName")) {
                            entity.setName(jsonObject.getString("bankName"));
                        }
                        if (!jsonObject.isNull("dailyTradingLimit")) {
                            entity.setDailyTradingLimit(jsonObject.getString("dailyTradingLimit"));
                        }
                        if (!jsonObject.isNull("monthlyTransactionLimit")) {
                            entity.setMonthlyTransactionLimit(jsonObject.getString("monthlyTransactionLimit"));
                        }
                        if (!jsonObject.isNull("singleTransactionLimit")) {
                            entity.setSingleTransactionLimit(jsonObject.getString("singleTransactionLimit"));
                        }
                        if (!jsonObject.isNull("bankCode")) {
                            entity.setId(jsonObject.getString("bankCode"));
                        }
                        list.add(entity);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
